import wepy from 'wepy'

export default class Home extends wepy.mixin {
  data = {
    queryInfo:{
      query:'',
      cid:'',
      pagenum:1,
      pagesize:10
    },
    //商品列表
    goodsList:[],
    //商品总数
    total:0,
    //触底节流阀
    isLoading:false,
    //是否已加载完全部数据
    isHidMore:true
  }

  config = {
  }

  methods = {
    goGoodsDetail(id){
      wepy.navigateTo({
        url:"/pages/goods_detail/main?goods_id=" + id
      })
    }
  }

  async getGoodsList(fn){
    //此时发起了请求 将this.isLoading改为true
    this.isLoading = true
    const {data} = await wepy.get('/goods/search',this.queryInfo)
    if(data.meta.status !== 200) return wepy.baseToast();
    this.goodsList = [...this.goodsList,...data.message.goods]
    this.total = data.message.total
    fn && fn()
    //数据获取完毕 将isloading改为false
    this.isLoading = false
    this.$apply();
  }

  onLoad(option){
    this.queryInfo.query = option.query || ""
    this.queryInfo.cid = option.id || ""
    this.getGoodsList()
  }

  //滚动条触底
  onReachBottom(){
    // 当isloading为true时，就表示正在获取数据 此时就return 阻止再次获取数据
    if(this.isLoading) return
    //判断是否已经数据全部加载完成 判断条件 this.pagenum * this.pagesize >= this.total
    if(this.queryInfo.pagenum * this.queryInfo.pagesize >= this.total) return this.isHidMore = false
    this.queryInfo.pagenum++
    this.getGoodsList();    
  }
  //下拉刷新
  onPullDownRefresh(){
    this.queryInfo.pagenum = 1;
    this.goodsList = [];
    this.isHidMore = true;
    this.getGoodsList(function(){
      wepy.stopPullDownRefresh();
    });
    
  }
}